import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import entry from "./entry"

export default new Vuex.Store({
  state: {
    // 用户信息
    user:{
      account:"123456",
      phone:"12345678910",
      password:"123456",
      token:"jhfajdfsagdjhs1654656",
      avatar:{
        nickname:"不爱吃🥕的兔子",
        imgUrl:"../assets/images/微信图片_20211112094433.jpg"
      },
      // 创作的数组
      creation:[]
    },
    userToken:null,
    // 记录密码登录页面用户输入的数据
    inpAcc:"",
    inpPass:"",

    // 记录验证码登录页面用户输入的数据
    inpPhone:"",
    inpCode:"",
    code:0,

    //搜索历史记录
    history:[] ,

    // 点赞数组
    likes:[],

    // 收藏词条的数组
    collects:[],
    
    // 记录切换前的地址
    fromPagePath:"",

    // 记录显示更多选项的布尔值(是否显示或隐藏)
    showChoiceFlag:false,

    // 滑动的距离
    scrollTop:0
  },
  mutations: {
    // 设置用户的token
    setUserToken(state,value){
      state.userToken=value;
    },
    // 清除所有历史记录
    clearAllHistory(state){
      state.history=[];
    },
    // 清除指定的历史记录
    clearHistory(state,index){
      state.history.splice(index,1);
    },
    // 添加历史记录
    addHistory(state,value){
      for (let index = 0; index < state.history.length; index++) {
        if(state.history[index].input == value){
          state.history.splice(index,1);
          break;
        }
      }
      let id = new Date().getTime();
      let obj={
        id,
        input:value
      }
      state.history.unshift(obj);
    },
    // 添加到喜欢的数组
    addLikes(state,obj){
      for (let index = 0; index < state.likes.length; index++) {
        if(state.likes[index].id == obj.id){
          state.likes.splice(index,1);
          break;
        }
      }
      state.likes.unshift(obj);
    },
    // 从喜欢数组中删除指定的词条
    removeLikes(state,id){
      for (let index = 0; index < state.likes.length; index++) {
        if(state.likes[index].id == id){
          state.likes.splice(index,1);
          break;
        }
      }
    },
    // 添加到收藏的数组
    addCollects(state,obj){
      for (let index = 0; index < state.collects.length; index++) {
        if(state.collects[index].id == obj.id){
          state.collects.splice(index,1);
          break;
        }
      }
      state.collects.unshift(obj);
    },
    // 从收藏数组中删除指定的词条
    removeCollects(state,id){
      for (let index = 0; index < state.collects.length; index++) {
        if(state.collects[index].id == id){
          state.collects.splice(index,1);
          break;
        }
      }
    },
    // 添加创作列表
    addCreation(state,obj){
      if(state.user.creation.length == 0){
        state.user.creation.push(obj);
        return;
      }
      state.user.creation.unshift(obj);
    },
    // 删除创作的词条
    removeCreationFromUser(state,id){
      for (let index = 0; index < state.user.creation.length; index++) {
        if(state.user.creation[index].id == id){
          state.user.creation.splice(index,1);
          break;
        }
      }
    },
    // 设置更多选项的布尔值
    setshowChoiceFlag(state,value){
      state.showChoiceFlag = value;
    },

    // 设置滑动的距离
    setScrollTop(state,value){
      state.scrollTop =value;
    }


  },
  actions: {
  },
  modules: {
    entry
  }
})
